﻿#include "Explorer.h"
#include <QMessageBox>

#include <QDockWidget>
#include <QMainWindow>
#include <QTreeView>
#include <QFileSystemModel>
#include <QFileDialog>
#include <QApplication>

#pragma execution_character_set("utf-8")
SlightExplorerClass::SlightExplorerClass(QWidget* parent)
	: QObject(parent), mainWidget_(parent)
{

	dockWidget_ = new QDockWidget(QString::fromUtf8("Explorer"), NULL);
	dockWidget_->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable);

	dockWidget_->setAllowedAreas(Qt::LeftDockWidgetArea);
	dockWidget_->hide();

	auto mainWindow = dynamic_cast<QMainWindow*>(parent);
	mainWindow->addDockWidget(Qt::LeftDockWidgetArea, dockWidget_);
    treeView = new TreeView();

    fileModel = new QFileSystemModel(this);
    fileModel->setRootPath(QDir::currentPath());
    treeView->setModel(fileModel);
    treeView->setObjectName(QString::fromUtf8("treeView"));
    treeView->setGeometry(QRect(0, 30, 321, 511));
    treeView->setContextMenuPolicy(Qt::CustomContextMenu);

    treeView->setHeaderHidden(true);
    // 隐藏其他三列，大小，名称等信息
    treeView->setColumnHidden(1, true);
    treeView->setColumnHidden(2, true);
    treeView->setColumnHidden(3, true);

    contextMenu = new QMenu();
    removeItemAction = new QAction("remove", contextMenu);
    cutItemAction = new QAction("cut", contextMenu);
    pasteItemAction = new QAction("paste", contextMenu);
   // newFolderAction = new QAction("new folder", contextMenu);

    contextMenu->addAction(removeItemAction);
    contextMenu->addAction(cutItemAction);
    contextMenu->addAction(pasteItemAction);
   // contextMenu->addAction(newFolderAction);

    connect(treeView, SIGNAL(customContextMenuRequested(QPoint)),
        this, SLOT(on_context_Menu_requested(QPoint)));
    //connect(newFolderAction, SIGNAL(clicked(bool)),
    //    this, SLOT(newFolder()));
    connect(removeItemAction, SIGNAL(triggered(bool)),
        this, SLOT(removeItem()));
    connect(cutItemAction, SIGNAL(triggered(bool)),
        this, SLOT(cutItem()));
    connect(pasteItemAction, SIGNAL(triggered(bool)),
        this, SLOT(pasteItem()));
}


void SlightExplorerClass::on_OpenShow()
{
    QString dirPath = QFileDialog::getExistingDirectory(NULL, tr("浏览选择文件夹"), tr("C:"));
    if (dirPath.isEmpty())//如果用户选择的路径为空
    {
        return;
    }
    else
    {
        auto rootModelIndex = fileModel->index(dirPath);
        treeView->setRootIndex(rootModelIndex); // 设置树视图的根索引为模型中的索引
        dockWidget_->setWidget(treeView);
        dockWidget_->show();
    }
}

void SlightExplorerClass::newFolder()
{
    QModelIndex index = treeView->currentIndex();
    QString name = "新建文件夹";
    fileModel->mkdir(index, name);
    treeView->expand(index);
}

void SlightExplorerClass::removeItem()
{
    QModelIndex index = treeView->currentIndex();
    fileModel->remove(index);
}

void SlightExplorerClass::cutItem()
{
    clipboard = new QModelIndex(treeView->currentIndex());
}

void SlightExplorerClass::pasteItem()
{
    if (!clipboard)
        return;

    QModelIndex index = treeView->currentIndex();

    QString sourcePath = fileModel->filePath(*clipboard);
    QString destinationPath = fileModel->filePath(index);

    QString bug = fileModel->filePath(clipboard->parent());
    qDebug() << bug;

    if (copy(sourcePath, destinationPath))
    {
        fileModel->remove(*clipboard);
        clipboard = 0;
    }
}

bool SlightExplorerClass::copy(const QString& source,
    const QString& destination)
{
    QFileInfo sourceInfo(source);

    if (sourceInfo.isDir())
    {
        QDir destDir(destination);
        if (!destDir.mkdir((QFileInfo(source).fileName())))
            return false;

        QDir sourceDir(source);
        QStringList fileNames = sourceDir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries);

        foreach(const QString & fileName, fileNames)
        {
            const QString newSrcPath = source + '/' + fileName;
            const QString newDestPath = destination + '/' + sourceDir.dirName();
            if (!copy(newSrcPath, newDestPath))
                return false;
        }
    }

    else
    {
        const QString dest = destination + QLatin1String("/") + QFileInfo(source).fileName();
        qDebug() << dest;
        if (!QFile::copy(source, dest))
            return false;
    }

    return true;
}

void SlightExplorerClass::on_context_Menu_requested(const QPoint& pos)
{
    QModelIndex index = treeView->indexAt(pos);
    if (index.isValid())
    {
        contextMenu->exec(QCursor::pos());
    }
}

SlightExplorerClass::~SlightExplorerClass()
{
}


